﻿Imports System.Math


Public Class Scheduler
    Dim CurrentRow As Integer = -1
    Dim oBindingSource As New BindingSource
    Dim iNbrCol As Integer
    Dim NeedValidate As Boolean
    Dim LockValidating As Boolean




    Sub New()

        ' Cet appel est requis par le Concepteur Windows Form.
        InitializeComponent()

        AddHandler DataGridView1.CellValueChanged, AddressOf DataGridView1_CellValueChanged


        oBindingSource.Add("test1")
        oBindingSource.Add("test2")
        oBindingSource.Add("test3")
        ' Ajoutez une initialisation quelconque après l'appel InitializeComponent().

    End Sub



    Sub Initialize(ByVal nbrCol As Integer, ByVal increment As Integer)
        Dim header As String
        Dim iHeure As Integer = 0
        Dim iMinute As Integer = 0
        iNbrCol = nbrCol
        Dim oComboColumNames As New DataGridViewComboBoxColumn
        oComboColumNames.HeaderText = "Nom"
        oComboColumNames.Name = "Nom"
        Dim oBindingSource As New BindingSource

        oBindingSource.Add("test1")
        oBindingSource.Add("test2")
        oBindingSource.Add("test3")
        oComboColumNames.DataSource = oBindingSource


        DataGridView1.Columns.Add(oComboColumNames)
        With DataGridView1.Columns("Nom")
            .Frozen = True
        End With


        For i = 0 To nbrCol - 1
            header = Floor(i / 4) & ":" & IIf(((i Mod 4) * 15) = 0, "00", (i Mod 4) * 15)
            DataGridView1.Columns.Add(i, header)
            DataGridView1.Columns(i.ToString).Width = 38
            'MsgBox(header)
        Next

    End Sub

    Sub EditName(ByVal strNom As String)
        DataGridView1.Rows(DataGridView1.SelectedCells(0).RowIndex).Cells("Nom").Value = strNom

    End Sub

    Private Sub DataGridView1_CellEndEdit(ByVal sender As Object, ByVal e As System.Windows.Forms.DataGridViewCellEventArgs) Handles DataGridView1.CellEndEdit
        'If CType(sender, DataGridView).SelectedCells(0).ColumnIndex = 0 Then
        '    DataGridView1.Rows(CType(sender, DataGridView).SelectedCells(0).RowIndex).Cells(0) = New DataGridViewTextBoxCell With {.Value = CType(sender, DataGridView).SelectedCells(0).Value}
        '    CurrentRow = DataGridView1.SelectedCells(0).RowIndex
        '    NeedValidate = True

        '    CType(CType(sender, DataGridView).SelectedCells(0), DataGridViewComboBoxCell).DataSource = oBindingSource
        '    oBindingSource.Remove(DataGridView1.Rows(CType(sender, DataGridView).SelectedCells(0).RowIndex).Cells("Nom"))
        'End If
    End Sub

    Private Sub DataGridView1_CellLeave(ByVal sender As Object, ByVal e As System.Windows.Forms.DataGridViewCellEventArgs) Handles DataGridView1.CellLeave
        'If CType(sender, DataGridView).SelectedCells(0).ColumnIndex = 0 Then
        '    LockValidating = True
        '    DataGridView1.Rows(CType(sender, DataGridView).SelectedCells(0).RowIndex).Cells(0) = New DataGridViewTextBoxCell With {.Value = CType(sender, DataGridView).SelectedCells(0).Value}
        '    CurrentRow = DataGridView1.SelectedCells(0).RowIndex
        '    NeedValidate = True

        '    CType(CType(sender, DataGridView).SelectedCells(0), DataGridViewComboBoxCell).DataSource = oBindingSource
        '    oBindingSource.Remove(DataGridView1.Rows(CType(sender, DataGridView).SelectedCells(0).RowIndex).Cells("Nom"))
        'End If
    End Sub

    Private Sub DataGridView1_CellValidating(ByVal sender As Object, ByVal e As System.Windows.Forms.DataGridViewCellValidatingEventArgs) Handles DataGridView1.CellValidating
        'DataGridView1.Rows(CType(sender, DataGridView).SelectedCells(0).RowIndex).Cells(0) = New DataGridViewTextBoxCell With {.Value = CType(sender, DataGridView).SelectedCells(0).Value}
        'CurrentRow = DataGridView1.SelectedCells(0).RowIndex
        'NeedValidate = True

        ''CType(CType(sender, DataGridView).SelectedCells(0), DataGridViewComboBoxCell).DataSource = oBindingSource
        'oBindingSource.Remove(DataGridView1.Rows(CType(sender, DataGridView).SelectedCells(0).RowIndex).Cells("Nom"))
    End Sub


    Private Sub DataGridView1_CellValueChanged(ByVal sender As Object, ByVal e As System.Windows.Forms.DataGridViewCellEventArgs) Handles DataGridView1.CellValueChanged
        If LockValidating Then
            Return
        End If
    End Sub

    Private Sub DataGridView1_RowEnter(ByVal sender As Object, ByVal e As System.Windows.Forms.DataGridViewCellEventArgs) Handles DataGridView1.RowEnter

        'If CType(sender, DataGridView).SelectedCells.Count <> 0 AndAlso CType(sender, DataGridView).SelectedCells(0).ColumnIndex = 0 Then
        '    DataGridView1.Rows(CType(sender, DataGridView).SelectedCells(0).RowIndex).Cells(0) = New DataGridViewTextBoxCell With {.Value = CType(sender, DataGridView).SelectedCells(0).Value}
        '    CurrentRow = DataGridView1.SelectedCells(0).RowIndex


        '    'CType(CType(sender, DataGridView).SelectedCells(0), DataGridViewComboBoxCell).DataSource = oBindingSource
        '    oBindingSource.Remove(CType(sender, DataGridView).SelectedCells(0).Value)
        'End If
    End Sub

    Private Sub DataGridView1_RowLeave(ByVal sender As Object, ByVal e As System.Windows.Forms.DataGridViewCellEventArgs) Handles DataGridView1.RowLeave
        'If CType(sender, DataGridView).SelectedCells(0).ColumnIndex = 0 Then
        '    DataGridView1.Rows(CType(sender, DataGridView).SelectedCells(0).RowIndex).Cells(0) = New DataGridViewTextBoxCell With {.Value = CType(sender, DataGridView).SelectedCells(0).Value}
        CurrentRow = DataGridView1.SelectedCells(0).RowIndex


        '    'CType(CType(sender, DataGridView).SelectedCells(0), DataGridViewComboBoxCell).DataSource = oBindingSource
        oBindingSource.Remove(CType(sender, DataGridView).SelectedCells(0).Value)
        'End If
    End Sub

    Private Sub DataGridView1_RowsAdded(ByVal sender As Object, ByVal e As System.Windows.Forms.DataGridViewRowsAddedEventArgs) Handles DataGridView1.RowsAdded
        CType(DataGridView1.Rows(CType(sender, DataGridView).Rows.Count - 1).Cells("Nom"), DataGridViewComboBoxCell).DataSource = Nothing
        CType(DataGridView1.Rows(CType(sender, DataGridView).Rows.Count - 1).Cells("Nom"), DataGridViewComboBoxCell).DataSource = oBindingSource

    End Sub

    Private Sub DataGridView1_RowValidated(ByVal sender As Object, ByVal e As System.Windows.Forms.DataGridViewCellEventArgs) Handles DataGridView1.RowValidated
        If Not CurrentRow = -1 Then
            DataGridView1.Rows(CurrentRow).Cells("Nom").ReadOnly = True
        End If
        setHoraire()
    End Sub

    Sub setHoraire()
        If Not CurrentRow = -1 Then
            If DataGridView1.Columns.Count > 1 Then
                Dim oNewRow As New ScheduleLine(iNbrCol)

                oNewRow.Horaire = "000111000121111102101111001"

                For j = 0 To iNbrCol - 1
                    DataGridView1.Rows(CurrentRow).Cells(j.ToString).Value = oNewRow.Horaire(j)
                    If DataGridView1.Rows(CurrentRow).Cells(j.ToString).Value = "1" Then
                        CType(DataGridView1.Rows(CurrentRow).Cells(j.ToString), DataGridViewTextBoxCell).Style.BackColor = Color.Aqua
                    End If
                    If DataGridView1.Rows(CurrentRow).Cells(j.ToString).Value = "2" Then
                        CType(DataGridView1.Rows(CurrentRow).Cells(j.ToString), DataGridViewTextBoxCell).Style.BackColor = Color.Red
                    End If
                Next
            End If
        End If

    End Sub


    Private Class ScheduleLine
        Dim oObject As Object
        Dim strHoraire As String
        Dim iNbrCol As Integer


        Sub New(ByVal nbrCol As Integer)
            iNbrCol = nbrCol
        End Sub

        Property Horaire() As String
            Get
                Return strHoraire
            End Get
            Set(ByVal value As String)
                For j = value.Length - 1 To iNbrCol
                    value += "0"
                Next
                strHoraire = value
            End Set
        End Property





    End Class


End Class


